package org.zsk.cs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zsk.cs.domain.po.HotArticle;
import org.zsk.cs.domain.vo.HotArticleVo;
import org.zsk.cs.response.ResultBody;
import org.zsk.cs.service.HotArticleService;
import org.zsk.fastdev.assign.Assign;

import java.util.List;

/**
 * <p>
 * 热点文章 controller
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/29
 */
@RestController
@RequestMapping("/hotarticle")
public class HotArticleController {

    private final HotArticleService hotArticleService;
    private final Assign assign;

    @Autowired
    public HotArticleController(HotArticleService hotArticleService, Assign assign) {
        this.hotArticleService = hotArticleService;
        this.assign = assign;
    }

    @PostMapping("/a")
    public ResultBody add(@RequestBody HotArticleVo hotArticleVo) throws Exception {
        HotArticle hotArticle=new HotArticle();
        assign.assign(hotArticleVo,hotArticle);
        hotArticleService.save(hotArticle);
        return ResultBody.success();
    }

    @PutMapping("/u")
    public ResultBody update(@RequestBody HotArticleVo hotArticleVo) throws Exception {
        HotArticle hotArticle=new HotArticle();
        assign.assign(hotArticleVo,hotArticle);
        hotArticleService.updateById(hotArticle);
        return ResultBody.success();
    }

    @DeleteMapping("/d/{ids}")
    public ResultBody delete(@PathVariable List<Integer> ids) throws Exception {
        hotArticleService.removeByIds(ids);
        return ResultBody.success();
    }

    @GetMapping("/g")
    public ResultBody page(HotArticleVo hotArticleVo) throws Exception {
        Page<HotArticle> page=new Page<>();
        assign.assign(hotArticleVo,page,"$current,$size");
        return ResultBody.success(hotArticleService.page(page));
    }
}
